﻿@{
    ViewBag.Title = "Home Page";
}
<link href="~/webuploader/webuploader.css" rel="stylesheet" />
<div style="margin-top:50px">
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-horizontal", id = "myform" }))
    {
        @*<div class="form-group">
            <div class="col-md-12">
                @if (ViewBag.msg != null && ViewBag.msg != "")
                {
                    <div class="alert alert-success text-center" role="alert">@ViewBag.msg</div>
                }
            </div>
        </div>*@
        <div class="form-group">
            <label for="grade" class="col-md-2 control-label">班级<span style="margin-left:5px;color:red">*</span></label>
            <div class="col-md-10">
                @Html.DropDownList("GradeId", ViewBag.grade as IEnumerable<SelectListItem>, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <label for="studentName" class="col-md-2 control-label">姓名<span style="margin-left:5px;color:red">*</span></label>
            <div class="col-md-10">
                @Html.DropDownList("StudentId", ViewBag.student as IEnumerable<SelectListItem>, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <label for="studentName" class="col-md-2 control-label">课本<span style="margin-left:5px;color:red">*</span></label>
            <div class="col-md-10">
                @Html.DropDownList("Book", ViewBag.book as IEnumerable<SelectListItem>, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <label for="studentName" class="col-md-2 control-label">章节<span style="margin-left:5px;color:red">*</span></label>
            <div class="col-md-10">
                @Html.TextBox("chapter", null, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <label for="studentName" class="col-md-2 control-label">作业源码</label>
            <div class="col-md-10">
                <div id="uploader" class="wu-example">
                    <!--用来存放文件信息-->
                    <div id="thelist" class="uploader-list"></div>
                    <div class="btns">
                        <div id="picker">选择文件</div>
                        <input id="ctlBtn" type="button" value="开始上传" class="btn btn-warning" />
                        <input type="hidden" value="" id="SoundCodePath" name="SoundCodePath" />
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label for="studentName" class="col-md-2 control-label">未完成原因</label>
            <div class="col-md-10">
                @Html.TextArea("Reason", new { @class = "form-control", rows = "5" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-10 col-md-offset-2">
                <button class="btn btn-success btn-lg">提交</button>
            </div>
        </div>
    }
</div>
@section scripts{
    <script src="~/webuploader/webuploader.js"></script>
    <script>
        $(function () {
            $('#GradeId').change(function () {
                if ($(this)) {
                    if ($(this).val() != 0) {
                        $.get(
                            '/home/getStudent',
                            { gradId: $(this).val() },
                            function (result) {
                                var myHTML = "";
                                $('#StudentId').html("");//赋值之前先清空
                                $.each(result, function (i, data) {
                                    myHTML += "<option value=" + data.Value + ">" + data.Text + "</option>";

                                });
                                $('#StudentId').append(myHTML);
                            }
                        );
                    }
                }

            });
            $('#myform').submit(function () {
                let GradeId = $('#GradeId').val();
                let StudentId = $('#StudentId').val();
                let Book = $('#Book').val();
                let chapter = $('#chapter').val();
                if (GradeId == 0 || StudentId == 0 && Book == 0) {
                    alert('班级、学生、课本都是必填项！')
                    return false;
                }
                if (!chapter) {
                    alert('章节是必填项！')
                    return false;
                }
                let SoundCodePath = $('#SoundCodePath').val();
                let Reason = $('#Reason').val();
                if (SoundCodePath == Reason) {
                    alert('作业源码、未完成原因二选一')
                    return false;
                }
            })
        });
    </script>
    <script>
        var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../";
        var GUID = WebUploader.Base.guid();//一个GUID
        $(function () {
            var $ = jQuery;
            var $list = $('#thelist');
            var uploader = WebUploader.create({

                // 选完文件后，是否自动上传。
                auto: false,
                // swf文件路径
                swf: applicationPath + '../webuploader/Uploader.swf',


                // 文件接收服务端。
                server: 'Home/Upload',

                // 选择文件的按钮。可选。
                // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick: '#picker',

                chunked: true,//开始分片上传
                chunkSize: 2048000,//每一片的大小
                formData: {
                    guid: GUID //自定义参数，待会儿解释
                },

                // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
                resize: false
            });
            // 当有文件被添加进队列的时候
            uploader.on('fileQueued', function (file) {
                $list.append('<div id="' + file.id + '" class="item">' +
                    '<h4 class="info">' + file.name + '</h4>' +
                    '<p class="state">等待上传...</p>' +
                    '</div>');
            });
            // 文件上传过程中创建进度条实时显示。
            uploader.on('uploadProgress', function (file, percentage) {
                var $li = $('#' + file.id),
                    $percent = $li.find('.progress .progress-bar');

                // 避免重复创建
                if (!$percent.length) {
                    $percent = $('<div class="progress progress-striped active">' +
                        '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                        '</div>' +
                        '</div>').appendTo($li).find('.progress-bar');
                }

                $li.find('p.state').text('上传中');

                $percent.css('width', percentage * 100 + '%');
            });

            // 文件上传成功，给item添加成功class, 用样式标记上传成功。
            uploader.on('uploadSuccess', function (file, response) {

                $('#' + file.id).find('p.state').text('已上传');
                $.post('Home/Merge', { guid: GUID, fileName: file.name }, function (data) {
                    $("#uploader .state").html("上传成功...");
                    $('#SoundCodePath').val(data.finalPath);
                    console.info(data.finalPath)
                });
            });

            // 文件上传失败，显示上传出错。
            uploader.on('uploadError', function (file) {
                $('#' + file.id).find('p.state').text('上传出错');
            });

            // 完成上传完了，成功或者失败，先删除进度条。
            uploader.on('uploadComplete', function (file) {
                $('#' + file.id).find('.progress').fadeOut();
            });

            //所有文件上传完毕
            uploader.on("uploadFinished", function (data) {
                //提交表单
                console.log(data)
            });
            //开始上传
            $("#ctlBtn").click(function () {
                uploader.upload();

            });

        });
    </script>
}
